<style type="text/css">
.status-row{
  cursor: pointer;
}
#role-actors-head{
  float:left;
  width:100%;
}
#role-actors-head ul{
  list-style-type:none;
  margin:0px;
  padding:0px;
  font-weight:bold;
  font-size: 12px;
}
#role-actors-head ul li{
  border-bottom: 1px solid #DDD;
  float:left;
  margin:5px;
  width:140px;
}
#sortable-role-actors {
  list-style-type:none;
  margin: 0px;
  padding: 0px;
}
.role-actors-list{
  float:left;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  border-radius: 3px;
  width:100%;
  border: 1px solid #DDD;
  background: #EEE;
  margin-bottom: 5px;
  /*cursor: move; */
}
.role-actors-list ul{
  list-style-type:none;
  margin:0px;
  padding:0px;
}
.role-actors-list ul li{
  float:left;
  margin:5px;
  width:140px;
}

.role-actors-list ul li:last-child {
  width:20px;
  cursor: pointer;
}
.role-option{
  width:50px;
}
#role-info{
  display:none;
  border:1px solid #DDD;
  padding:5px;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  border-radius: 4px;
  margin-bottom:5px;
}

#role-actors-block{
  float:left;
  padding:5px;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  border-radius: 4px;
  border:1px solid #DDD;
}

.role-actors-list.even-row{
  /*background: #CCC; */
}

.role-actors-list.odd-row{

}
.actor-inherit{
  font-style: italic;
}
</style>

<script type="text/javascript">
$(document).ready(function(){

	var g_shareable = $('input[name=shareable]').is(':checked') ? 1 : 0;
	var g_show_ref = $('input[name=show_ref]').is(':checked') ? 1 : 0;
	var g_show_company = $('input[name=show_company]').is(':checked') ? 1 : 0;
	var g_show_rate = $('input[name=show_rate]').is(':checked') ? 1 : 0;
	var g_show_date = $('input[name=show_date]').is(':checked') ? 1 : 0;

        var reorder_actors = 0;
        var actors_count = <?php echo ($this->actors_count)? $this->actors_count : 0;?>;
	
	$("#role-actors-view").click(function(event){
          $('#mapactor-pop-up').css('display','none');
          $('#facade').css('display','none');
          $('#mapactor-pop-up').html('');
          if(reorder_actors)
            location.reload();
        });

	$('.editable').editable('/matter/save-matter-actor/', {
		submitdata: function(value,settings){
            reorder_actors = 1;
			var mal_id = $(this).parent().attr('id').split('-');
			return { matter_actor_id: mal_id[2] }
		},
          cancel : false,
          submit : false,
          select : true,
          indicator : '<span class="icon-busy" />',
          tooltip: 'Click to edit...'
	});
	
	$.editable.addInputType('autocomplete', {
		element : $.editable.types.text.element,
		plugin : function(settings, original){
		  $('input', this).autocomplete(settings.autocomplete);
	  }
	});

    var company_id = 0;
	$('.editable_comp').editable("/matter/save-matter-actor/", {
		type : "autocomplete",
		onblur : "submit",
		autocomplete : {
			minLength: 1,
			source : "/matter/get-all-actors/",
			autoFocus: true,
			select: function( event, ui ) {
				company_id = ui.item.id;
			}
		},
		submitdata: function(value,settings){
			var mal_id = $(this).attr('id').split('-');
			reorder_actors = 1;
			return { 
				matter_actor_id: mal_id[2], 
				field: 'company_ID', 
				val: company_id
			};
		},
		cancel : false,
		submit : false,
		indicator : '<span class="icon-busy" />',
		tooltip: 'Click to edit...',
		select: true
	});

    var actor_id = 0;
	$('.editable_actor').editable("/matter/save-matter-actor/", {
		type: "autocomplete",
		onblur: "submit",
		autocomplete: {
			source: "/matter/get-all-actors/",
			autoFocus: true,
			select: function( event, ui ) {
				actor_id = ui.item.id;
			}
		},
		submitdata: function(value,settings){
			reorder_actors = 1;
			return { 
				matter_actor_id: $(this).parent().parent().parent().attr('id'), 
				field: 'actor_ID', 
				val: actor_id
			};
		},
		cancel : false,
		submit : false,
		indicator : '<span class="icon-busy" />',
		tooltip: 'Click to edit...',
		select: true
	});

	$('#edit-role-info').click(function(){
		$('#role-actors-block').css('display', 'none');
        $('#actor-role-title').html("Edit general role information");
		$('#role-info').css('display', 'block');
	});

	$('input[name=cancel_role]').click(function(){
    $('#role-info').css('display', 'none');
    $('#actor-role-title').html("Actor role");
    $('#role-actors-block').css('display', 'block');
    if(g_shareable)
    	$('input[name=shareable]').attr('checked', 'checked');
    else
    	$('input[name=shareable]').removeAttr('checked');
	
    if(g_show_ref)
    	$('input[name=show_ref]').attr('checked', 'checked');
    else
    	$('input[name=show_ref]').removeAttr('checked');

    if(g_show_company)
    	$('input[name=show_company]').attr('checked', 'checked');
    else
    	$('input[name=show_company]').removeAttr('checked');

    if(g_show_rate)
    	$('input[name=show_rate]').attr('checked', 'checked');
    else
    	$('input[name=show_rate]').removeAttr('checked');

    if(g_show_date)
    	$('input[name=show_date]').attr('checked', 'checked');
    else
    	$('input[name=show_date]').removeAttr('checked');
	});

	$('input[name=save_role]').click(function(){
		var shareable = $('input[name=shareable]').is(':checked') ? 1 : 0;
		var show_ref = $('input[name=show_ref]').is(':checked') ? 1 : 0;
		var show_company = $('input[name=show_company]').is(':checked') ? 1 : 0;
		var show_rate = $('input[name=show_rate]').is(':checked') ? 1 : 0;
		var show_date = $('input[name=show_date]').is(':checked') ? 1 : 0;
		var role_notes = $('textarea[name="role_notes"]').val();
		var role_code = $('input[name=role_code]').val();

		$.ajax({
			type: "POST",
			url: "/matter/save-role/",
			data: { 'role_code': role_code, 'shareable': shareable, 'show_ref':show_ref, 'show_company':show_company, 'show_rate':show_rate, 'show_date':show_date, 'notes':role_notes },
			success: function(data){
			//	alert("role info saved!");
				$('#role-info').css('display', 'none');
				
				if(show_ref)
					$('.actor_ref').css('display', 'block');
				else
					$('.actor_ref').css('display', 'none');

				if(show_company)
					$('.actor_comp').css('display', 'block');
				else
					$('.actor_comp').css('display', 'none');

				if(show_rate)
					$('.actor_rate').css('display', 'block');
				else
					$('.actor_rate').css('display', 'none');

				if(show_date)
					$('.actor_date').css('display', 'block');
				else
					$('.actor_date').css('display', 'none');
				
                                $('#actor-role-title').html("Actor role");
				$('#role-actors-block').css('display', 'block');
				g_shareable = shareable;
				g_show_ref = show_ref;
				g_show_company = show_company;
				g_show_rate = show_rate;
				g_show_date = show_date;
			}
	  });

	});

	$('.delete-matter-actor').click(function(){
		var actor_list = $(this).parent();
		var mal_id = $(this).parent().attr('id').split('-');
        var matter_id = $('#matter-id').val();
        var actor_mid = $(this).attr('id');
		$('#pr-confirm').dialog({
			autoOpen: false,
			width: 400,
			modal: true,
			resizable: false,
			title: "phpIP info",
			buttons: {
			   "Delete": function(){
			     $.ajax({
				     type:"POST",
				     url: "/matter/delete-matter-actor",
				     data: { 'mal_id':mal_id[2] },
				     success: function(data){
                       if(!isNaN(parseInt(data))){
					     $('#pr-confirm').dialog("close");
					     $('#'+mal_id[2]).hide();
                         reorder_actors = 1;
                         actors_count = actors_count - 1;
                         if(actors_count < 2)
                           $('#sortable-role-actors').sortable({disabled: true});
                         }else{
                           alert(data);
                         }
				     }
				 });
		       },
	       	   "Cancel": function(){
			       $(this).dialog("close");
		       }
		    }
		});
        if(matter_id == actor_mid)
		    $('#pr-confirm').text("The actor will be deleted from the current matter only");
        else
		    $('#pr-confirm').text("The actor will be deleted from all matters in the family");
		$('#pr-confirm').dialog("open");
	});

    if(actors_count > 1){
	$('#sortable-role-actors').sortable({
            update : function(event, ui){
              var order = $(this).sortable('serialize');
              var liIds = $('.role-actors-list').map(function(i,n){
                  return $(n).attr('id');
              }).get().join(',');

              $.ajax({
                type: 'POST',
                url : '/matter/save-display-order/',
                data: { order_list : liIds },
                success : function(data){
                  reorder_actors = 1;
                }
              });
            }
        });
	    $('#sortable-role-actors').disableSelection();
      }

    if(actors_count == 1){
       $('.sort-icon').hide();
    }

    $('button, input:button').button();
    $('#check').button();
    $('#flag').buttonset();
});
</script>

<div class="listrow" style="width:100%">
<div class="status-row" id="role-actors-view">
<span class="eventlist-col-name" style="margin-left: 5px;" id="actor-role-title">Role<span class="ui-icon ui-icon-arrowreturnthick-1-s" style="float: right;"</span></span>
</div>
<div class="list-matter-block-info" style="max-height: 350px; min-height: 100px; width:99%; height: auto;" >
<input type="hidden" value="<?=$this->matter_id?>" name="matter_id" id="matter-id" />
<?php if(isset($this->actor_role)): ?>

<div style="width:160px"><?php echo $this->actor_role['name'] . " (" .$this->actor_role['code'].")"?><a href="javascript:void(0);" id="edit-role-info" title="Configure fields to display for this role"><span class="ui-icon ui-icon-wrench" style="float: right;"></span></a></div>
<input type="hidden" value="<?=$this->actor_role['code']?>" name="role_code"/>
<div id="role-info">
<div id="flag">
<input type="checkbox" name="shareable" id="shareable" <?php echo ($this->actor_role['shareable'])? 'checked="checked"': ''; ?>></input><label for="shareable">Shareable</label>
<input type="checkbox" name="show_ref" id="show_ref" <?php echo ($this->actor_role['show_ref'])? 'checked="checked"': ''; ?>></input><label for="show_ref">Show ref</label>
<input type="checkbox" name="show_company" id="show_company" <?php echo ($this->actor_role['show_company'])? 'checked="checked"': ''; ?>></input><label for="show_company">Show company</label>
<input type="checkbox" name="show_rate" id="show_rate" <?php echo ($this->actor_role['show_rate'])? 'checked="checked"': ''; ?> ></input><label for="show_rate">Show rate</label>
<input type="checkbox" name="show_date" id="show_date" <?php echo ($this->actor_role['show_date'])? 'checked="checked"': ''; ?> ></input><label for="show_date">Show date</label>
</div>
<br><br><label style="vertical-align:top;">Notes :</label><textarea name="role_notes" cols="80" rows="2"><?=$this->actor_role['notes']?></textarea>
<br><br><input type="button" value="Save" name="save_role" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="Cancel" name="cancel_role" />
</div>
<?php endif;?>
<div id="role-actors-block">

<div id="role-actors-head"><ul>
<li style="width: 156px;">Name</li>
<li <?php echo ($this->actor_role['show_ref'])? '' : 'style="display:none;"';?> class="actor_ref" >Actor ref</li>
<li <?php echo ($this->actor_role['show_company'])? '' : 'style="display:none;"';?> class="actor_comp" >Company</li>
<li <?php echo ($this->actor_role['show_rate'])? 'style="width:70px;"' : 'style="display:none;"';?> class="actor_rate" >Rate</li>
<li <?php echo ($this->actor_role['show_date'])? 'style="width:70px;"' : 'style="display:none;"';?> class="actor_date" >Date</li>
<li style="border:none;" ><img src="" alt="" width=16 height=16 title="Delete Actor"></img></li>
</ul></div>
<ul id="sortable-role-actors" >
<?php if(isset($this->role_actors)):
$i=0;
foreach($this->role_actors as $actor):
$i++;
//print_r($actor);
$row_class = 'odd-row';
if($i%2 == 0)
  $row_class = 'even-row';
?>
<li class="role-actors-list <?=$row_class?>" id="<?=$actor['ID']?>">
<ul id="matter-actor-<?=$actor['ID']?>" class="<?php echo ($actor['inherited'])?"actor-inherit":"";?>">
<?php 
$actor_display_name = ($actor['display_name'] == "" ) ? $actor['name'] .  (($actor['first_name']) ? (", " . $actor['first_name']) : "") : $actor['display_name'];
?>
<li style="width: 156px;"><span class="ui-icon ui-icon-arrowthick-2-n-s sort-icon" style="float:left; cursor: move;" ></span><span class="editable_actor" id="actor_ID"><?php echo htmlentities($actor_display_name); ?></span></li>
<li class="editable actor_ref" <?php echo ($actor['show_ref'])? '' : 'style="display:none;"';?> id="actor_ref" ><?=$actor['actor_ref']?></li>
<li class="editable_comp actor_comp" <?php echo ($actor['show_company'])? '' : 'style="display:none;"';?> id="company_ID-<?=$actor['company_ID']?>-<?=$actor['ID']?>" ><?=$actor['company_name']?></li>
<li class="editable actor_rate" <?php echo ($actor['show_rate'])? 'style="width:70px;"' : 'style="display:none;"';?> id="rate" ><?=$actor['rate']?></li>
<li class="editable actor_date" <?php echo ($actor['show_date'])? 'style="width:70px;"' : 'style="display:none;"';?> id="date" ><?=$actor['date']?></li>
<li class="delete-matter-actor" id="<?=$actor['matter_ID']?>"><span class="ui-icon ui-icon-trash" style="float:right;"></span></li>
</ul>
</li>
<?php 
endforeach;
endif;?>
</ul>
</div>

</div>
</div>

<div id="pr-confirm"></div>
